﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace Collect_Pub
{
    public class App
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="from">发送者邮箱</param>
        /// <param name="fromer">发送人</param>
        /// <param name="to">接受者邮箱</param>
        /// <param name="toer">收件人</param>
        /// <param name="Subject">主题</param>
        /// <param name="Body">内容</param>
        /// <param name="file">附件</param>
        /// <param name="SMTPHost">smtp服务器</param>
        /// <param name="SMTPuser">邮箱</param>
        /// <param name="SMTPpass">密码</param>

        /// <returns></returns>
        public static string Mail_Send(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody,  List<string>  sfiles, string sSMTPHost, string sSMTPuser, string sSMTPpass)
        {

           // sBody = sBody.Replace("\r\n", "<br>");
        ////设置from和to地址
            MailAddress from = new MailAddress(sfrom, sfromer);
        //MailAddress to = new MailAddress();
        ////创建一个MailMessage对象
        MailMessage oMail = new MailMessage();
            oMail.From=from;
            string[] lsto = sto.Split(';');
            for (int i = 0; i < lsto.Length; i++)
			{
                oMail.To.Add(lsto[i].ToString());
			}
        //// 添加附件
        if (sfiles.Count>0)
        {
            for (int i = 0; i < sfiles.Count; i++)
            {
                oMail.Attachments.Add(new Attachment(sfiles[i].ToString()));
                
            }
            
        }
        ////邮件标题
        oMail.SubjectEncoding = Encoding.UTF8;
        oMail.IsBodyHtml = true;//是否是HTML邮件 
        oMail.Subject = sSubject;
        ////邮件内容
        oMail.Body = sBody;
        ////邮件格式
        oMail.IsBodyHtml = false;
        ////邮件采用的编码
        oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
        ////设置邮件的优先级为高
        oMail.Priority = MailPriority.High;
        ////发送邮件
        SmtpClient client = new SmtpClient();
        ////client.UseDefaultCredentials = false; 
        client.Host = sSMTPHost;
        client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        try
        {
            client.Send(oMail);
            return "TRUE";
        }
        catch (Exception ex)
        {
            
            return ex.Message.ToString();
        }
        finally
        {
            ////释放资源
            oMail.Dispose();
        }

        
        }


        /// <summary>
        /// 32位MD5加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string MD5Encrypt32(string password)
        {
            byte[] result = Encoding.Default.GetBytes(password);    //tbPass为输入密码的文本框
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "");  //tbMd5pass为输出加密文本的文本框
           
        }


        public static void ErrLog(string lsex)
        {
            string lspath = System.IO.Path.Combine(Application.StartupPath, "log");
            if (System.IO.Directory.Exists(lspath)==false)
            {
                System.IO.Directory.CreateDirectory(lspath);
            }
            string lsfile = System.IO.Path.Combine(lspath, System.DateTime.Now.ToString("yyyymmddhhmmss")+".txt");
            System.IO.File.WriteAllText(lsfile, lsex);
        }

       
       
    }
}
